package com.esri.core.tasks.geodatabase;

import com.esri.core.ags.FeatureServiceInfo;
import com.esri.core.geodatabase.Geodatabase;
import com.esri.core.geodatabase.GeodatabaseEditError;
import com.esri.core.geodatabase.GeodatabaseFeatureTableEditErrors;
import com.esri.core.internal.tasks.PollingHelper;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.internal.tasks.ags.ap;
import com.esri.core.internal.tasks.ags.aq;
import com.esri.core.internal.tasks.ags.ar;
import com.esri.core.internal.tasks.ags.r;
import com.esri.core.internal.tasks.b.b;
import com.esri.core.internal.tasks.b.c;
import com.esri.core.internal.tasks.b.d;
import com.esri.core.internal.tasks.e;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.ServerError;
import com.esri.core.map.ServerFile;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class GeodatabaseSyncTask {
    public static final String GENERATE_GDB_REQUEST_RECOVERY_INFO_DIR = "GenerateGdbRequest";
    private static final String GEODATABASE_PATH_RECOVERY_FILENAME = "GeodatbasePath";
    private static final String GEODATABASE_STATUS_RECOVERY_FILENAME = "GeodatabaseStatus";
    private static final String REQUEST_PARAMETERS_RECOVERY_FILENAME = "RequestParameters";
    public static final String SYNC_GDB_REQUEST_RECOVERY_INFO_DIR = "SyncGdbRequest";
    private static final String SYNC_PROCESS_RECOVERY_FILENAME = "SyncProcessRecoveryInfo";
    private static final String UPLOAD_FAILED = "Upload Failed";
    private UserCredentials _credentials;
    private String _recoveryDir;
    private String _serviceUrl;

    public GeodatabaseSyncTask(String str, UserCredentials userCredentials) {
        this._serviceUrl = str;
        this._credentials = userCredentials;
    }

    public Future<String> generateGeodatabase(GenerateGeodatabaseParameters generateGeodatabaseParameters, String str, boolean z, GeodatabaseStatusCallback geodatabaseStatusCallback, CallbackListener<String> callbackListener) {
        if (this._recoveryDir == null) {
            this._recoveryDir = new File(str).getParent();
        }
        return generateGeodatabase(generateGeodatabaseParameters, str, z, geodatabaseStatusCallback, callbackListener, generateRecoveryDirectoryName(generateGeodatabaseParameters));
    }

    private Future<String> generateGeodatabase(final GenerateGeodatabaseParameters generateGeodatabaseParameters, final String str, final boolean z, final GeodatabaseStatusCallback geodatabaseStatusCallback, final CallbackListener<String> callbackListener, final File file) {
        return e.b.submit(new Callable<String>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.1
            /* JADX WARN: Removed duplicated region for block: B:17:0x0047  */
            /* JADX WARN: Removed duplicated region for block: B:20:0x0070  */
            @Override // java.util.concurrent.Callable
            /*
                Code decompiled incorrectly, please refer to instructions dump.
                To view partially-correct code enable 'Show inconsistent code' option in preferences
            */
            public java.lang.String call() throws java.lang.Exception {
                /*
                    r7 = this;
                    java.io.File r0 = r2
                    if (r0 != 0) goto Lc
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r0 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters r1 = r3
                    java.io.File r0 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$000(r0, r1)
                Lc:
                    java.io.File r1 = new java.io.File
                    java.lang.String r2 = "GeodatabaseStatus"
                    r1.<init>(r0, r2)
                    r3 = 1
                    boolean r4 = r1.exists()
                    r5 = 0
                    if (r4 == 0) goto L44
                    boolean r4 = r4
                    if (r4 == 0) goto L3d
                    java.io.FileInputStream r4 = new java.io.FileInputStream
                    r4.<init>(r1)
                    org.codehaus.jackson.JsonParser r1 = com.esri.core.internal.util.d.a(r4)
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo r1 = com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo.fromJson(r1)
                    r4.close()
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo$Status r4 = r1.getStatus()
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo$Status r6 = com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo.Status.FAILED
                    if (r6 == r4) goto L45
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo$Status r6 = com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo.Status.COMPLETED_WITH_ERRORS
                    if (r6 == r4) goto L45
                    r3 = 0
                    goto L45
                L3d:
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r1 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    java.io.File r4 = r2
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$100(r1, r4)
                L44:
                    r1 = r5
                L45:
                    if (r3 == 0) goto L54
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r1 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters r3 = r3
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask$1$1 r4 = new com.esri.core.tasks.geodatabase.GeodatabaseSyncTask$1$1
                    r4.<init>()
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo r1 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$300(r1, r3, r4)
                L54:
                    com.esri.core.tasks.geodatabase.PollAndFetchGeodatabaseTask r3 = new com.esri.core.tasks.geodatabase.PollAndFetchGeodatabaseTask
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r4 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    com.esri.core.io.UserCredentials r4 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$400(r4)
                    r3.<init>(r1, r4)
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback r1 = r7
                    com.esri.core.map.CallbackListener r4 = r6
                    r3.setStatusCallback(r1, r4)
                    java.lang.Thread r1 = java.lang.Thread.currentThread()
                    boolean r1 = r1.isInterrupted()
                    if (r1 != 0) goto Lb7
                    java.lang.Object r1 = r3.call()
                    com.esri.core.map.ServerFile r1 = (com.esri.core.map.ServerFile) r1
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo r3 = r3.getStatus()
                    boolean r4 = r3.succeeded()
                    if (r4 == 0) goto L9d
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r2 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    java.lang.String r3 = r5
                    com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback r4 = r7
                    com.esri.core.map.CallbackListener r5 = r6
                    java.lang.String r5 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$500(r2, r3, r4, r5, r1)
                    java.io.File r1 = new java.io.File
                    r1.<init>(r5)
                    boolean r1 = r1.exists()
                    if (r1 == 0) goto Lb7
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r1 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$100(r1, r0)
                    goto Lb7
                L9d:
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask r0 = com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.this
                    java.io.File r1 = r2
                    java.lang.String r4 = r3.toJson()
                    com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.access$600(r0, r1, r4, r2)
                    com.esri.core.map.CallbackListener r0 = r6
                    if (r0 == 0) goto Lb7
                    com.esri.core.map.ServerError r1 = r3.getError()
                    java.lang.Throwable r1 = r1.getThrowable()
                    r0.onError(r1)
                Lb7:
                    return r5
                */
                throw new UnsupportedOperationException("Method not decompiled: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.AnonymousClass1.call():java.lang.String");
            }
        });
    }

    public Future<Map<Integer, GeodatabaseFeatureTableEditErrors>> syncGeodatabase(SyncGeodatabaseParameters syncGeodatabaseParameters, Geodatabase geodatabase, GeodatabaseStatusCallback geodatabaseStatusCallback, CallbackListener<Map<Integer, GeodatabaseFeatureTableEditErrors>> callbackListener) {
        if (this._recoveryDir == null) {
            this._recoveryDir = new File(geodatabase.getPath()).getParent();
        }
        return submitSyncJobAndApplyResults(syncGeodatabaseParameters, geodatabase, geodatabaseStatusCallback, callbackListener, generateRecoveryDirectoryName(syncGeodatabaseParameters));
    }

    private Future<Map<Integer, GeodatabaseFeatureTableEditErrors>> submitSyncJobAndApplyResults(final SyncGeodatabaseParameters syncGeodatabaseParameters, final Geodatabase geodatabase, final GeodatabaseStatusCallback geodatabaseStatusCallback, final CallbackListener<Map<Integer, GeodatabaseFeatureTableEditErrors>> callbackListener, final File file) {
        return e.b.submit(new Callable<Map<Integer, GeodatabaseFeatureTableEditErrors>>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.2
            @Override // java.util.concurrent.Callable
            public Map<Integer, GeodatabaseFeatureTableEditErrors> call() throws Exception {
                String str;
                Throwable th;
                File file2 = file;
                if (file2 == null) {
                    file2 = GeodatabaseSyncTask.this.generateRecoveryDirectoryName(syncGeodatabaseParameters);
                }
                File file3 = new File(file2, GeodatabaseSyncTask.SYNC_PROCESS_RECOVERY_FILENAME);
                if (!file3.exists()) {
                    GeodatabaseSyncTask.this.generateGdbSyncRecoveryInfo(file2, syncGeodatabaseParameters, geodatabase.getPath());
                }
                final SyncProcessRecoveryInfo syncProcessRecoveryInfo = new SyncProcessRecoveryInfo(file3);
                if (Thread.currentThread().isInterrupted()) {
                    return null;
                }
                if (GeodatabaseSyncTask.this.isNotDownloadOnly(syncGeodatabaseParameters)) {
                    str = GeodatabaseSyncTask.this.submitUploadForSyncJobInternal(geodatabase, syncGeodatabaseParameters, new CallbackListener<String>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.2.1
                        @Override // com.esri.core.map.CallbackListener
                        public void onCallback(String str2) {
                        }

                        @Override // com.esri.core.map.CallbackListener
                        public void onError(Throwable th2) {
                            if (callbackListener != null) {
                                callbackListener.onError(th2);
                            }
                        }
                    }, syncProcessRecoveryInfo);
                    if (str != null && str.equals(GeodatabaseSyncTask.UPLOAD_FAILED)) {
                        return null;
                    }
                } else {
                    syncProcessRecoveryInfo.setUploadClientDeltaDone(null);
                    str = null;
                }
                if (Thread.currentThread().isInterrupted()) {
                    return null;
                }
                GeodatabaseStatusInfo sumbitSyncJobInternal = GeodatabaseSyncTask.this.sumbitSyncJobInternal(str, syncGeodatabaseParameters, new CallbackListener<GeodatabaseStatusInfo>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.2.2
                    @Override // com.esri.core.map.CallbackListener
                    public void onCallback(GeodatabaseStatusInfo geodatabaseStatusInfo) {
                    }

                    @Override // com.esri.core.map.CallbackListener
                    public void onError(Throwable th2) {
                        if (callbackListener != null) {
                            callbackListener.onError(th2);
                        }
                    }
                }, syncProcessRecoveryInfo);
                if (Thread.currentThread().isInterrupted()) {
                    return null;
                }
                PollingHelper pollingHelper = new PollingHelper();
                pollingHelper.a(geodatabaseStatusCallback, callbackListener);
                GeodatabaseStatusInfo geodatabaseStatusInfo = (GeodatabaseStatusInfo) pollingHelper.a(1000L, sumbitSyncJobInternal.getUpdateUrl(), GeodatabaseSyncTask.this._credentials, GeodatabaseStatusInfo.Status.PENDING, new GeodatabaseStatusPollHelper());
                if (Thread.currentThread().isInterrupted()) {
                    return null;
                }
                if (geodatabaseStatusInfo.succeeded()) {
                    return GeodatabaseSyncTask.this.fetchAndSyncServerDelta(syncGeodatabaseParameters, geodatabaseStatusInfo, geodatabaseStatusCallback, new CallbackListener<Map<Integer, GeodatabaseFeatureTableEditErrors>>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.2.3
                        @Override // com.esri.core.map.CallbackListener
                        public void onCallback(Map<Integer, GeodatabaseFeatureTableEditErrors> map) {
                            String clientDeltaPath = syncProcessRecoveryInfo.getClientDeltaPath();
                            if (clientDeltaPath != null) {
                                File file4 = new File(clientDeltaPath);
                                if (file4.exists()) {
                                    file4.delete();
                                }
                            }
                            syncProcessRecoveryInfo.cleanupRecoveryInfo();
                            if (callbackListener != null) {
                                callbackListener.onCallback(map);
                            }
                        }

                        @Override // com.esri.core.map.CallbackListener
                        public void onError(Throwable th2) {
                            if (callbackListener != null) {
                                callbackListener.onError(th2);
                            }
                        }
                    }, geodatabase, syncProcessRecoveryInfo);
                }
                syncProcessRecoveryInfo.setSyncFailed();
                if (callbackListener != null) {
                    ServerError error = geodatabaseStatusInfo.getError();
                    if (error != null) {
                        th = error.getThrowable();
                    } else {
                        th = new Throwable("Unknown error");
                    }
                    callbackListener.onError(th);
                    return null;
                }
                return null;
            }
        });
    }

    public FeatureServiceInfo fetchFeatureServiceInfo() throws Exception {
        return new r(this._serviceUrl, this._credentials).execute();
    }

    public Future<FeatureServiceInfo> fetchFeatureServiceInfo(final CallbackListener<FeatureServiceInfo> callbackListener) {
        return e.b.submit(new r(this._serviceUrl, this._credentials, new TaskListener<FeatureServiceInfo>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.3
            @Override // com.esri.core.internal.tasks.TaskListener
            public void onError(Throwable th) {
                CallbackListener callbackListener2 = callbackListener;
                if (callbackListener2 != null) {
                    callbackListener2.onError(th);
                }
            }

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onCompletion(short s, FeatureServiceInfo featureServiceInfo) {
                CallbackListener callbackListener2 = callbackListener;
                if (callbackListener2 != null) {
                    callbackListener2.onCallback(featureServiceInfo);
                }
            }
        }));
    }

    public Future<GeodatabaseStatusInfo> registerGeodatabase(final Geodatabase geodatabase, final GeodatabaseStatusCallback geodatabaseStatusCallback, final CallbackListener<GeodatabaseStatusInfo> callbackListener) throws IOException, Exception {
        return e.b.submit(new Callable<GeodatabaseStatusInfo>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.4
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.util.concurrent.Callable
            public GeodatabaseStatusInfo call() throws Exception {
                if (geodatabase.isSyncEnabled()) {
                    PollAndFetchGeodatabaseTask pollAndFetchGeodatabaseTask = new PollAndFetchGeodatabaseTask(GeodatabaseSyncTask.this.submitGenerateGeodatabaseJobInternal(geodatabase.getRegisterParameters(), new CallbackListener<GeodatabaseStatusInfo>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.4.1
                        @Override // com.esri.core.map.CallbackListener
                        public void onError(Throwable th) {
                        }

                        @Override // com.esri.core.map.CallbackListener
                        public void onCallback(GeodatabaseStatusInfo geodatabaseStatusInfo) {
                            if (geodatabaseStatusCallback != null) {
                                geodatabaseStatusCallback.statusUpdated(geodatabaseStatusInfo);
                            }
                        }
                    }), GeodatabaseSyncTask.this._credentials);
                    pollAndFetchGeodatabaseTask.setStatusCallback(geodatabaseStatusCallback, callbackListener);
                    pollAndFetchGeodatabaseTask.call();
                    GeodatabaseStatusInfo status = pollAndFetchGeodatabaseTask.getStatus();
                    if (status.succeeded()) {
                        GeodatabaseStatusInfo execute = new b(status.getResultUrl(), GeodatabaseSyncTask.this._credentials).execute();
                        String geodatabaseId = execute.getGeodatabaseId();
                        if (geodatabaseId != null && geodatabaseId.length() > 0) {
                            SyncGeodatabaseParameters syncParameters = geodatabase.getSyncParameters();
                            syncParameters.setGeodatabaseId(geodatabaseId);
                            geodatabase.setSyncParameters(syncParameters);
                            geodatabase.reRegister(geodatabaseId, GeodatabaseSyncTask.this._credentials == null ? "" : GeodatabaseSyncTask.this._credentials.getUserName());
                            CallbackListener callbackListener2 = callbackListener;
                            if (callbackListener2 != null) {
                                callbackListener2.onCallback(execute);
                                return execute;
                            }
                            return execute;
                        }
                        CallbackListener callbackListener3 = callbackListener;
                        if (callbackListener3 != null) {
                            callbackListener3.onError(execute.getError().getThrowable());
                            return execute;
                        }
                        return execute;
                    }
                    CallbackListener callbackListener4 = callbackListener;
                    if (callbackListener4 != null) {
                        callbackListener4.onError(status.getError().getThrowable());
                        return status;
                    }
                    return status;
                }
                return null;
            }
        });
    }

    public Future<GeodatabaseTaskResult> unregisterGeodatabase(Geodatabase geodatabase, CallbackListener<GeodatabaseTaskResult> callbackListener) throws Exception {
        return unregisterGeodatabase(geodatabase.getSyncParameters().getGeodatabaseId(), callbackListener);
    }

    public Future<GeodatabaseTaskResult> unregisterGeodatabase(final String str, final CallbackListener<GeodatabaseTaskResult> callbackListener) {
        return e.b.submit(new Callable<GeodatabaseTaskResult>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.5
            /* JADX WARN: Can't rename method to resolve collision */
            @Override // java.util.concurrent.Callable
            public GeodatabaseTaskResult call() throws Exception {
                return new com.esri.core.internal.tasks.b.e(new UnregisterGeodatabaseParams(str), GeodatabaseSyncTask.this._serviceUrl, GeodatabaseSyncTask.this._credentials, new TaskListener<GeodatabaseTaskResult>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.5.1
                    @Override // com.esri.core.internal.tasks.TaskListener
                    public void onCompletion(short s, GeodatabaseTaskResult geodatabaseTaskResult) {
                        if (callbackListener != null) {
                            callbackListener.onCallback(geodatabaseTaskResult);
                        }
                    }

                    @Override // com.esri.core.internal.tasks.TaskListener
                    public void onError(Throwable th) {
                        if (callbackListener != null) {
                            callbackListener.onError(th);
                        }
                    }
                }).call();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public GeodatabaseStatusInfo submitGenerateGeodatabaseJobInternal(GenerateGeodatabaseParameters generateGeodatabaseParameters, final CallbackListener<GeodatabaseStatusInfo> callbackListener) {
        return new GeodatabaseStatusInfo(this._serviceUrl, new c(generateGeodatabaseParameters, this._serviceUrl, this._credentials, new TaskListener<String>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.6
            private boolean _errored;

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onError(Throwable th) {
                this._errored = true;
                th.printStackTrace();
                CallbackListener callbackListener2 = callbackListener;
                if (callbackListener2 != null) {
                    callbackListener2.onError(th);
                }
            }

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onCompletion(short s, String str) {
                CallbackListener callbackListener2;
                if (this._errored || s != 1 || (callbackListener2 = callbackListener) == null) {
                    return;
                }
                callbackListener2.onCallback(new GeodatabaseStatusInfo(GeodatabaseSyncTask.this._serviceUrl, str));
            }
        }).call());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String submitUploadForSyncJobInternal(Geodatabase geodatabase, SyncGeodatabaseParameters syncGeodatabaseParameters, final CallbackListener<String> callbackListener, SyncProcessRecoveryInfo syncProcessRecoveryInfo) {
        File parentFile = new File(geodatabase.getPath()).getParentFile();
        File file = new File(parentFile, "client_delta_" + System.currentTimeMillis() + ".geodatabase");
        String str = null;
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        if (syncProcessRecoveryInfo == null || !syncProcessRecoveryInfo.createdClientDelta()) {
            if (geodatabase.hasLocalEdits()) {
                try {
                    geodatabase.setSyncParameters(syncGeodatabaseParameters);
                    geodatabase.createClientDelta(file.getAbsolutePath());
                } catch (IOException e) {
                    if (callbackListener != null) {
                        callbackListener.onError(e);
                    }
                } catch (RuntimeException e2) {
                    if (callbackListener != null) {
                        callbackListener.onError(e2);
                    }
                }
            }
            if (syncProcessRecoveryInfo != null) {
                syncProcessRecoveryInfo.setCreatedClientDelta(file.getAbsolutePath());
            }
        } else if (syncProcessRecoveryInfo != null) {
            file = new File(syncProcessRecoveryInfo.getClientDeltaPath());
        }
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        aq aqVar = ((syncProcessRecoveryInfo == null || !syncProcessRecoveryInfo.uploadClientDeltaDone()) && file.exists()) ? new aq(new ar(file, "application/octet-stream"), this._serviceUrl, this._credentials, new TaskListener<ap>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.7
            private boolean _errored = false;

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onError(Throwable th) {
                this._errored = true;
                th.printStackTrace();
                CallbackListener callbackListener2 = callbackListener;
                if (callbackListener2 != null) {
                    callbackListener2.onError(th);
                }
            }

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onCompletion(short s, ap apVar) {
                CallbackListener callbackListener2;
                if (this._errored || (callbackListener2 = callbackListener) == null) {
                    return;
                }
                callbackListener2.onCallback(apVar.b());
            }
        }) : null;
        if (Thread.currentThread().isInterrupted()) {
            return null;
        }
        if (syncProcessRecoveryInfo != null && syncProcessRecoveryInfo.uploadClientDeltaDone()) {
            if (syncProcessRecoveryInfo != null) {
                return syncProcessRecoveryInfo.getUploadId();
            }
            return null;
        }
        String str2 = UPLOAD_FAILED;
        if (aqVar != null) {
            ap call = aqVar.call();
            if (call != null && call.a() && call.f()) {
                str2 = call.b();
            }
            str = str2;
        }
        if (syncProcessRecoveryInfo == null || str == null) {
            return str;
        }
        syncProcessRecoveryInfo.setUploadClientDeltaDone(str);
        return str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public String downloadGeodatabase(String str, final GeodatabaseStatusCallback geodatabaseStatusCallback, CallbackListener<String> callbackListener, ServerFile serverFile) {
        if (!Thread.currentThread().isInterrupted()) {
            try {
                File file = new File(str);
                if (geodatabaseStatusCallback != null) {
                    serverFile.setProgressListener(new ServerFile.ProgressListener() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.8
                        @Override // com.esri.core.map.ServerFile.ProgressListener
                        public void reportProgress(String str2, long j, long j2) {
                            geodatabaseStatusCallback.statusUpdated(new GeodatabaseStatusInfo(j, j2));
                        }
                    });
                }
                serverFile.download(file.getParentFile());
                if (file.exists() && !file.delete()) {
                    str = serverFile.getPath();
                    callbackListener.onError(new RuntimeException("File " + file.getPath() + " already exists and could not be deleted."));
                }
                if (!new File(serverFile.getPath()).renameTo(file) && callbackListener != null) {
                    str = serverFile.getPath();
                    callbackListener.onError(new RuntimeException("Failed to rename downloaded geodatabase file (" + serverFile.getPath() + ") to " + file.getPath() + "."));
                }
                if (callbackListener != null) {
                    callbackListener.onCallback(str);
                }
            } catch (EsriSecurityException e) {
                if (callbackListener != null) {
                    callbackListener.onError(e);
                }
            } catch (IOException e2) {
                if (callbackListener != null) {
                    callbackListener.onError(e2);
                }
            } catch (Exception e3) {
                if (callbackListener != null) {
                    callbackListener.onError(e3);
                }
            }
        }
        return str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Code restructure failed: missing block: B:76:0x013f, code lost:
        if (r15 != null) goto L48;
     */
    /* JADX WARN: Removed duplicated region for block: B:124:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:85:0x0150  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public java.util.Map<java.lang.Integer, com.esri.core.geodatabase.GeodatabaseFeatureTableEditErrors> fetchAndSyncServerDelta(com.esri.core.tasks.geodatabase.SyncGeodatabaseParameters r10, com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo r11, final com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback r12, com.esri.core.map.CallbackListener<java.util.Map<java.lang.Integer, com.esri.core.geodatabase.GeodatabaseFeatureTableEditErrors>> r13, com.esri.core.geodatabase.Geodatabase r14, com.esri.core.tasks.geodatabase.SyncProcessRecoveryInfo r15) throws java.lang.Exception {
        /*
            Method dump skipped, instructions count: 436
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.fetchAndSyncServerDelta(com.esri.core.tasks.geodatabase.SyncGeodatabaseParameters, com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo, com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback, com.esri.core.map.CallbackListener, com.esri.core.geodatabase.Geodatabase, com.esri.core.tasks.geodatabase.SyncProcessRecoveryInfo):java.util.Map");
    }

    private Map<Integer, GeodatabaseFeatureTableEditErrors> generateEditErrorsMap(JsonParser jsonParser) throws JsonParseException, IOException, Exception {
        GeodatabaseEditError fromJson;
        HashMap hashMap = new HashMap();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT && (fromJson = GeodatabaseEditError.fromJson(jsonParser)) != null) {
                int layerId = fromJson.getLayerId();
                GeodatabaseFeatureTableEditErrors geodatabaseFeatureTableEditErrors = (GeodatabaseFeatureTableEditErrors) hashMap.get(Integer.valueOf(layerId));
                if (geodatabaseFeatureTableEditErrors == null) {
                    geodatabaseFeatureTableEditErrors = new GeodatabaseFeatureTableEditErrors();
                    hashMap.put(Integer.valueOf(layerId), geodatabaseFeatureTableEditErrors);
                }
                if (fromJson.isAttachment()) {
                    geodatabaseFeatureTableEditErrors.addAttachmentError(fromJson);
                } else if (fromJson != null) {
                    geodatabaseFeatureTableEditErrors.addFeatureError(fromJson);
                }
            }
        }
        return hashMap;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isNotDownloadOnly(SyncGeodatabaseParameters syncGeodatabaseParameters) throws Exception {
        if (syncGeodatabaseParameters.getSyncModel() == SyncModel.GEODATABASE) {
            if (syncGeodatabaseParameters.getSyncDirection() != SyncDirection.DOWNLOAD) {
                return true;
            }
        } else {
            Iterator<LayerSyncInfo> it = syncGeodatabaseParameters.getSyncLayers().iterator();
            while (it.hasNext()) {
                if (it.next().getSyncDirection() != SyncDirection.DOWNLOAD) {
                    return true;
                }
            }
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public File generateRecoveryDirectoryName(GenerateGeodatabaseParameters generateGeodatabaseParameters) {
        File file = new File(this._recoveryDir, GENERATE_GDB_REQUEST_RECOVERY_INFO_DIR);
        return new File(file, Integer.valueOf(generateGeodatabaseParameters.hashCode()).toString() + "_" + this._serviceUrl.hashCode());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public File generateRecoveryDirectoryName(SyncGeodatabaseParameters syncGeodatabaseParameters) {
        return new File(new File(this._recoveryDir, SYNC_GDB_REQUEST_RECOVERY_INFO_DIR), Integer.valueOf(syncGeodatabaseParameters.hashCode()).toString());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void generateGdbRequestRecoveryInfo(File file, GenerateGeodatabaseParameters generateGeodatabaseParameters, GeodatabaseStatusInfo geodatabaseStatusInfo, String str) {
        try {
            if (!file.exists()) {
                file.mkdirs();
            }
            writeRecoveryFile(file, geodatabaseStatusInfo.toJson(), GEODATABASE_STATUS_RECOVERY_FILENAME);
            writeRecoveryFile(file, generateGeodatabaseParameters.toJson(), REQUEST_PARAMETERS_RECOVERY_FILENAME);
            writeRecoveryFile(file, str, GEODATABASE_PATH_RECOVERY_FILENAME);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void writeRecoveryFile(File file, String str, String str2) {
        PrintStream printStream = null;
        try {
            try {
                PrintStream printStream2 = new PrintStream(new File(file, str2));
                try {
                    printStream2.print(str);
                    printStream2.close();
                } catch (FileNotFoundException e) {
                    e = e;
                    printStream = printStream2;
                    e.printStackTrace();
                    if (printStream != null) {
                        printStream.close();
                    }
                } catch (Throwable th) {
                    th = th;
                    printStream = printStream2;
                    if (printStream != null) {
                        printStream.close();
                    }
                    throw th;
                }
            } catch (FileNotFoundException e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void generateGdbSyncRecoveryInfo(File file, SyncGeodatabaseParameters syncGeodatabaseParameters, String str) {
        try {
            if (!file.exists()) {
                file.mkdirs();
            }
            writeRecoveryFile(file, str, GEODATABASE_PATH_RECOVERY_FILENAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void cleanupRecoveryInfo(File file) {
        if (file == null || !file.exists() || Thread.currentThread().isInterrupted()) {
            return;
        }
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            for (File file2 : listFiles) {
                file2.delete();
            }
        }
        file.delete();
        File parentFile = file.getParentFile();
        if (parentFile.listFiles().length == 0) {
            parentFile.delete();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public GeodatabaseStatusInfo sumbitSyncJobInternal(String str, SyncGeodatabaseParameters syncGeodatabaseParameters, final CallbackListener<GeodatabaseStatusInfo> callbackListener, SyncProcessRecoveryInfo syncProcessRecoveryInfo) {
        GeodatabaseStatusInfo geodatabaseStatusInfo;
        Exception e;
        syncGeodatabaseParameters.setUploadId(str);
        if (syncProcessRecoveryInfo != null && syncProcessRecoveryInfo.syncComplete()) {
            if (syncProcessRecoveryInfo != null) {
                return new GeodatabaseStatusInfo(this._serviceUrl, syncProcessRecoveryInfo.getSyncStatusUrl());
            }
            return null;
        }
        try {
            String call = new d(syncGeodatabaseParameters, this._serviceUrl, this._credentials, new TaskListener<String>() { // from class: com.esri.core.tasks.geodatabase.GeodatabaseSyncTask.10
                @Override // com.esri.core.internal.tasks.TaskListener
                public void onCompletion(short s, String str2) {
                }

                @Override // com.esri.core.internal.tasks.TaskListener
                public void onError(Throwable th) {
                    CallbackListener callbackListener2 = callbackListener;
                    if (callbackListener2 != null) {
                        callbackListener2.onError(th);
                    }
                }
            }).call();
            geodatabaseStatusInfo = new GeodatabaseStatusInfo(this._serviceUrl, call);
            if (callbackListener != null) {
                try {
                    callbackListener.onCallback(geodatabaseStatusInfo);
                } catch (Exception e2) {
                    e = e2;
                    if (callbackListener != null) {
                        callbackListener.onError(e);
                    }
                    return geodatabaseStatusInfo;
                }
            }
            if (syncProcessRecoveryInfo != null) {
                syncProcessRecoveryInfo.setSyncComplete(call);
            }
        } catch (Exception e3) {
            geodatabaseStatusInfo = null;
            e = e3;
        }
        return geodatabaseStatusInfo;
    }
}
